package com.naiking.manager
{
	import com.naiking.data.HashMap;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	/**
	 * @author Nai.King
	 * 创建时间：2013-10-12 上午11:03:12
	 * 给显示对象Sprite加拖拽功能
	 */
	public class DragManager
	{
		private static var _dragDisMap:HashMap=new HashMap;
		/**
		 *添加拖拽 sprite 利用了sp自身的startDrag
		 * @param dis 对象Sprite
		 * @param _height 头部高度区
		 * 
		 */		
		public static function addDrag(dis:DisplayObject,_height:int=28):void
		{
			if(_dragDisMap.containsKey(dis))
				return;
			_dragDisMap.add(dis.name,_height);
			dis.addEventListener(MouseEvent.MOUSE_DOWN,onDrag);
			dis.addEventListener(MouseEvent.MOUSE_UP,onDragOut);
		}
		public static function removeDrag(dis:DisplayObject):void
		{
			if(_dragDisMap.containsKey(dis))
			{
				dis.removeEventListener(MouseEvent.MOUSE_DOWN,onDrag);
				dis.removeEventListener(MouseEvent.MOUSE_UP,onDragOut);
				_dragDisMap.remove(dis);
			}
		}
		private static function onDragOut(e:MouseEvent):void
		{
			(e.currentTarget as Sprite).stopDrag();
		}
		private static function onDrag(e:MouseEvent):void
		{
			var dis:Sprite=e.currentTarget as Sprite;
			var ary:Array=_dragDisMap.getValues();
			var h:int=_dragDisMap.getValue(dis.name);
			if(e.currentTarget.mouseX<dis.width&&e.currentTarget.mouseX>0
				&&e.currentTarget.mouseY>0&&e.currentTarget.mouseY<h)
			{
				dis.startDrag();
			}
		}
		public function DragManager()
		{
		}
	}
}